﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSP.Common.Web;
using CSP.Model.CSPBBS;
using CSP.Bll.IObjectBlls;
using CSP.Bll;
using System.Collections.ObjectModel;
using CSP.Common.Tools;
using CSP.Model.CSPEnum;

namespace CSP.Web.UserControls
{
    public partial class ServiceShopInfo : CSPUserControlBase
    {
        private string _userName;

        private string _regDate = "";

        private int _authenticationType = 0;

        private string _email = "";

        private string _mobile = "";

        private string _contactWayJson = "";

        private string _authenticationIcon = "";

        private string _authenticationText = "";

        #region 属性

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName
        {
            get { return _userName; }
            set
            {
                _userName = value;
            }
        }

        /// <summary>
        /// 注册时间
        /// </summary>
        protected string RegDate
        {
            get { return _regDate; }
        }

        /// <summary>
        /// 认证类型
        /// </summary>
        protected int AuthenticationType
        {
            get { return _authenticationType; }
        }

        protected string AuthenticationText
        {
            get { return _authenticationText; }
        }

        /// <summary>
        /// 是否显示服务信誉度
        /// </summary>
        public bool IsShowServiceCredit
        {
            get { return U_SynthesizeUserCredit.IsShowServiceCredit; }
            set { U_SynthesizeUserCredit.IsShowServiceCredit = value; }
        }


        /// <summary>
        /// 是否显示消费信誉度
        /// </summary>
        public bool IsShowConsumeCredit
        {
            get { return U_SynthesizeUserCredit.IsShowConsumeCredit; }
            set { U_SynthesizeUserCredit.IsShowConsumeCredit = value; }
        }


        /// <summary>
        /// 是否显示服务能力
        /// </summary>
        public bool IsServiceAbility
        {
            get { return U_SynthesizeUserCredit.IsServiceAbility; }
            set { U_SynthesizeUserCredit.IsServiceAbility = value; }
        }


        /// <summary>
        /// 是否显示服务态度
        /// </summary>
        public bool IsServiceAttitude
        {
            get { return U_SynthesizeUserCredit.IsServiceAttitude; }
            set { U_SynthesizeUserCredit.IsServiceAttitude = value; }
        }

        /// <summary>
        /// 布局
        /// </summary>
        public CSP.Web.UserControls.SynthesizeUserCredit.CSPLayout LayoutStyle
        {
            get { return U_SynthesizeUserCredit.LayoutStyle; }
            set { U_SynthesizeUserCredit.LayoutStyle = value; }
        }

        /// <summary>
        /// Email
        /// </summary>
        protected string Email
        {
            get { return _email; }
        }

        /// <summary>
        /// 移动电话
        /// </summary>
        protected string Mobile
        {
            get { return _mobile; }
        }

        /// <summary>
        /// 获取联系方式的前台UI json序列化字符串
        /// </summary>
        protected string ContactWayJson
        {
            get { return _contactWayJson; }
        }

        /// <summary>
        /// 认证图标
        /// </summary>
        protected string AuthenticationIcon
        {
            get { return _authenticationIcon; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            U_SynthesizeUserCredit.UserName = _userName;
            if (!IsPostBack)
            {
                SetUserInfo();
                SetContactWay();
            }
        }

        private void SetUserInfo()
        {
            Member member = GetUserInfoByBll();
            int courseCount = GetOnLineStatusCountByUser(_userName);
            if (member != null)
            {
                _userName = member.UserName;
                this.LB_UserName.Text = _userName;
                _regDate = member.RegDate.ToString("yyyy-MM-dd");
                this.LB_RegDate.Text = _regDate;
                this.LB_CourseCount.Text = courseCount.ToString();

                _email = member.Email;
                if (member.MemberProfile != null)
                {
                    _mobile = member.MemberProfile.Mobile;
                }

             
                if (member.UserBasic != null)
                {
                    _authenticationIcon = GetStyleByAuthentication(member.UserBasic.PersonalAuthentication, member.UserBasic.EnterpriseAuthentication);
                    _authenticationText = GetCNNameByAuthentication(member.UserBasic.PersonalAuthentication, member.UserBasic.EnterpriseAuthentication);
                    this.A_AuthenticationType.Title = _authenticationText;
                    this.A_AuthenticationType.Attributes.Add("class", "ico " + _authenticationIcon);
                    if (_authenticationText != "未认证")
                    {
                        this.LB_Authenication.Attributes.Add("title", "已认证");
                        this.LB_Authenication.Attributes.Add("class", "ico-indentifed");
                    }
                    else {
                        this.LB_Authenication.Visible = false;
                    }
                    //_authenticationType = Convert.ToInt32(member.UserBasic.PersonalAuthentication);
                }
            }
        }

        /// <summary>
        /// 从Bll中获取当前用户的基本信息
        /// </summary>
        /// <returns></returns>
        private Member GetUserInfoByBll()
        {
            Member result = null;
            using (var userInfoBll = CSPBllFactory.GetInstance().GetUserInfoBll())
            {
                result = userInfoBll.GetUser(_userName, true);
            }
            return result;
        }

        private void SetContactWay()
        {
            ICollection<Model.CSP.ContactWay> contactWays = GetContactWaysByUser();

            if (contactWays == null || contactWays.Count == 0)
            {
                return;
            }

            ContactWayUI[] contactWayUI = (from contact in contactWays
                                           select new ContactWayUI
                                           {
                                               Address = contact.address,
                                               Type = contact.type
                                           }).ToArray();
            _contactWayJson = CommonTool.SerializeObject(contactWayUI);
            this.LB_ContactWayJson.InnerHtml = _contactWayJson;
            //if (contactWays == null)
            //{
            //    contactWays = new Collection<Model.CSP.ContactWay>();
            //}
            //this.R_ContactWay.DataSource = contactWays;
            //this.R_ContactWay.DataBind();
        }

        private ICollection<Model.CSP.ContactWay> GetContactWaysByUser()
        {
            ICollection<Model.CSP.ContactWay> result = null;
            CSPBllFactory _factory = CSPBllFactory.GetInstance();
            using (var contactWayBll = _factory.GetContactWayBll())
            {
                result = contactWayBll.FindByUser(_userName);
            }

            return result;
        }

        /// <summary>
        /// 获取当前在线数量的课程
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private int GetOnLineStatusCountByUser(string userName)
        {
            CSPBllFactory _factory = CSPBllFactory.GetInstance();
            using (var serviceInfoBll = _factory.GetServiceInfoBll())
            {
                return serviceInfoBll.GetCountByUserAndStatus(userName,ServiceInfoStatus.OnLine);
            }
        }
        /// <summary>
        /// 获取联系方式链接字符串
        /// </summary>
        /// <param name="address">API 的地址</param>
        /// <param name="contactType">0旺旺,1QQ</param>
        /// <param name="urlType">0图片，1URL</param>
        /// <returns></returns>
        protected string GetContactURL(string address, int contactType, int urlType)
        {
            string result = "";
            if (contactType == 0 && urlType == 0)
            {
                result = CSP.Common.Tools.CommonTool.GetTaoBaoContactImgURL(address);
            }
            else if (contactType == 0 && urlType == 1)
            {
                result = CSP.Common.Tools.CommonTool.GetTaoBaoContactURL(address);
            }
            else if (contactType == 1 && urlType == 0)
            {
                result = CSP.Common.Tools.CommonTool.GetQQContactImgURL(address);
            }
            else if (contactType == 1 && urlType == 1)
            {
                result = CSP.Common.Tools.CommonTool.GetQQContactURL(address);
            }

            return result;
        }

        private string GetStyleByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            //企业认证
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-enterprise";
            }

            //个人认证
            else if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "i-v-personal";
            }
            else
            {
                return "i-v-unautherized";
            }
        }

        private string GetCNNameByAuthentication(int personalAuthentication, int enterpriseAuthentication)
        {
            if ((AuthenticationStatus)enterpriseAuthentication == AuthenticationStatus.Accomplish)
            {
                return "企业认证";
            }

            if ((AuthenticationStatus)personalAuthentication == AuthenticationStatus.Accomplish)
            {
                return "个人认证";
            }

            return "未认证";
        }

        private class ContactWayUI
        {
            public string Address { get; set; }

            public int Type { get; set; }
        }
    }
}